using FirstTea.Application.DTOs;
using FirstTea.Application.Interfaces;
using FirstTea.Domain.Entities;
using FirstTea.Domain.Repositories;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace FirstTea.Application.Services
{
    public class UserService : IUserService
    {
        private readonly IRepository<User> _userRepo;
        public UserService(IRepository<User> userRepo)
        {
            _userRepo = userRepo;
        }
        public async Task<UserDto?> GetByIdAsync(Guid id)
        {
            if (id == Guid.Empty) throw new ArgumentException("用户Id不能为空");
            var user = await _userRepo.GetByIdAsync(id);
            if (user == null) return null;
            return new UserDto { Id = user.Id, Username = user.Username, Email = user.Email, Phone = user.Phone };
        }
        public async Task<IEnumerable<UserDto>> GetAllAsync()
        {
            var users = await _userRepo.GetAllAsync();
            return users.Select(u => new UserDto { Id = u.Id, Username = u.Username, Email = u.Email, Phone = u.Phone });
        }
        public async Task CreateAsync(UserDto dto)
        {
            if (dto == null) throw new ArgumentNullException(nameof(dto));
            var user = new User { Username = dto.Username, Email = dto.Email, Phone = dto.Phone };
            await _userRepo.AddAsync(user);
            await _userRepo.SaveChangesAsync();
        }
        public async Task UpdateAsync(UserDto dto)
        {
            if (dto == null) throw new ArgumentNullException(nameof(dto));
            var user = await _userRepo.GetByIdAsync(dto.Id);
            if (user == null) return;
            user.Username = dto.Username;
            user.Email = dto.Email;
            user.Phone = dto.Phone;
            await _userRepo.UpdateAsync(user);
            await _userRepo.SaveChangesAsync();
        }
        public async Task DeleteAsync(Guid id)
        {
            await _userRepo.DeleteAsync(id);
            await _userRepo.SaveChangesAsync();
        }
        public async Task ResetPasswordAsync(Guid userId, string newPassword)
        {
            if (userId == Guid.Empty) throw new ArgumentException("用户Id不能为空");
            if (string.IsNullOrWhiteSpace(newPassword) || newPassword.Length < 6)
                throw new ArgumentException("新密码长度不能小于6位");
            var user = await _userRepo.GetByIdAsync(userId);
            if (user == null) throw new Exception("用户不存在");
            user.SetPassword(newPassword);
            await _userRepo.UpdateAsync(user);
            await _userRepo.SaveChangesAsync();
        }
        public async Task UpdateAvatarAsync(Guid userId, string avatarUrl)
        {
            if (userId == Guid.Empty) throw new ArgumentException("用户Id不能为空");
            if (string.IsNullOrWhiteSpace(avatarUrl)) throw new ArgumentException("头像路径不能为空");
            var user = await _userRepo.GetByIdAsync(userId);
            if (user == null) throw new Exception("用户不存在");
            user.AvatarUrl = avatarUrl;
            await _userRepo.UpdateAsync(user);
            await _userRepo.SaveChangesAsync();
        }
    }
}
